package com.xteam.youin_supplier.adpter;

import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.model.TruckTeamList;


/**
 * Created by Curry on 2015-12-16.
 */
public class TruckTeamListViewAdapter extends RecyclerView.Adapter<TruckTeamListViewAdapter.ViewHolder> implements View.OnClickListener {

    private Fragment mContext;
    private TruckTeamList truckTeamList;
    public TruckTeamListViewAdapter(Fragment context, TruckTeamList truckTeamList) {
        mContext = context;
        this.truckTeamList = truckTeamList;
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view, (int) view.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public TruckTeamListViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.truck_team_list_title_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(TruckTeamListViewAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        holder.getTruckTeamName().setText(truckTeamList.getData().get(position).getName());
    }

    @Override
    public int getItemCount() {
        return truckTeamList.getData().size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView truckImg;
        private TextView truckTeamName;
        private RelativeLayout layout;
        public ViewHolder(View itemView) {
            super(itemView);
            truckImg = (ImageView) itemView.findViewById(R.id.truck_img);
            truckTeamName = (TextView) itemView.findViewById(R.id.team_name);
            layout = (RelativeLayout) itemView.findViewById(R.id.layout);
        }

        public ImageView getTruckImg() {
            return truckImg;
        }

        public void setTruckImg(ImageView truckImg) {
            this.truckImg = truckImg;
        }

        public TextView getTruckTeamName() {
            return truckTeamName;
        }

        public void setTruckTeamName(TextView truckTeamName) {
            this.truckTeamName = truckTeamName;
        }

        public RelativeLayout getLayout() {
            return layout;
        }
    }
}
